package org.hhh.org.service.impl;

import org.hhh.basic.service.impl.BaseServiceImpl;
import org.hhh.basic.util.MD5Utils;
import org.hhh.basic.util.StrUtils;
import org.hhh.org.domain.Employee;
import org.hhh.org.mapper.EmployeeMapper;
import org.hhh.org.service.IEmployeeService;
import org.hhh.user.domain.Logininfo;
import org.hhh.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    //基础的CRUD业务实现通过继承
    //扩展的业务实现需要自己写
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    public void add(Employee employee) {
        //初始化字段
        initEmployee(employee);
        //拷贝logininfo
        Logininfo logininfo=employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);

    }

    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        //使用盐值加密
        String md5 = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5);


    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);//后台员工
        return logininfo;
    }

    @Override
    public void upadate(Employee employee) {
        super.upadate(employee);
    }

    @Override
    public void del(Long id) {
        super.del(id);
    }
}
